package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;



@Controller
@RequestMapping(value = "/uploadPhoto")
public class UploadStudyPhoto extends BaseController {

	@Autowired
	private BizService bizService;
	
	@Autowired
	private BizDao bizDao;
	
	private static Logger logger = LoggerFactory.getLogger(UploadStudyPhoto.class);

	
	/**
	 * 学习照片上传、保存
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addStudyPhoto", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addStudyPhoto(HttpServletRequest request)
			throws UnsupportedEncodingException {
		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		try {
			//学员ID
			String studentId = request.getParameter("studentId");
			//拍照时间
			String photoTime = URLDecoder.decode(request.getParameter("photoTime"),"utf-8");
			//视频播放开始时间的时间戳
			String classId = request.getParameter("classId");
			//学员编号
			String stuNum = request.getParameter("stuNum");
			//类型
			String eventType = request.getParameter("eventType");
			//照片64进制数据
			String image64Base = request.getParameter("image64Base");
			
			if (studentId != null && !studentId.isEmpty()
					&& photoTime != null && !photoTime.isEmpty() 
					&& classId != null && !classId.isEmpty()
					&& stuNum != null && !stuNum.isEmpty()
					&& eventType != null && !eventType.isEmpty()
					&& image64Base != null && !image64Base.isEmpty()) {
				// 保存图片到文件夹
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				paramsMap.put("studentId", studentId);
				paramsMap.put("photoTime", photoTime);
				paramsMap.put("classId", classId);
				paramsMap.put("stuNum", stuNum);
				paramsMap.put("eventType", eventType);
				paramsMap.put("image64Base", image64Base);
				response = bizService.addStudyPhoto(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("上传异常" + e.toString());
			response.setErrorcode(1);
			response.setMessage("操作异常！");
		} finally {
			return response.toJSON();
		}
	}
}
